<%@page language="java" pageEncoding="utf-8"%>
<%@page import="inet.util.DateProc"%>
<%@page import="java.util.Vector"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="inet.util.StringTool"%>
<%@page import="lib.db.clip.ClipCategory"%>

<jsp:useBean id="clipDAO" class="lib.db.clip.ClipDAO" scope="session"/>
<jsp:useBean id="categoryDAO" class="lib.db.clip.ClipCategoryDAO" scope="session"/>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Danh mục clip</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  	<link rel="icon" href="<%=request.getContextPath() %>/favicon.ico" type="image/x-icon" />
    <link href="../include/default.css" type="text/css" rel="stylesheet" />
	<script src="../include/default.js" type="text/javascript"></script>
    <script type="text/javascript">
	  	function popupwindow(url, title, w, h) {
		  	var left = (screen.width/2)-(w/2);
		  	var top = (screen.height/2)-(h/2);
		  	return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
		}
  	</script>
  	<style type="text/css">
  		.responderButton{
  			background-color: #5F4C0B; color: #fff; border: 1px solid #fff; 
  			border-radius: 5px; cursor: pointer; height: 25px;
  		}
  	</style>
  </head>  
  <body marginheight="0" topmargin="0" marginwidth="0" leftmargin="0" rightmargin="0">
  	<%@ include file="../include/checklogin.jsp" %>
  	<%
  		request.setCharacterEncoding("utf-8");
  		String sAction = request.getParameter("action");
  		String msg = "";
  		if("add_category".equalsIgnoreCase(sAction)){
  			String sCateName = request.getParameter("txtName");
  			String sPosition = request.getParameter("txtPosition");
  			int position = 0;
  			if(sPosition != null && StringTool.isNumberic(sPosition)) {
  			   position = Integer.parseInt(sPosition);
  			}
  			if(sCateName != null && !"".equals(sCateName)) {
  				categoryDAO.insertRow(sCateName, position);
  			}else {
  				msg = "<span style='color:red'>Bạn phải nhập tên danh mục</span>";
  			}
  		}
  		else if("delete".equalsIgnoreCase(sAction)){
			String sCateId = request.getParameter("id");
    		BigDecimal cateId = null;
    		if(sCateId != null && StringTool.isNumberic(sCateId)) {
    			cateId = new BigDecimal(sCateId);
    			if(!categoryDAO.hasClipInCate(cateId)){
    				if(categoryDAO.deleteRow(cateId)) {
    					msg = "<span style='color:blue'>Xóa danh mục thành công</span>";
    				} else {
    					msg = "<span style='color:red'>Không xóa được danh mục</span>";
    				}
    			} else {
    				msg = "<span style='color:red'>Đã có clip trong danh mục. vui lòng xóa clip của danh mục trước khi xóa DANH MỤC</span>";
    			}
    		}
		}
		
		Vector vCate = categoryDAO.findAll(1, 50);
		
  	%>
  	<div class="list" style="margin-left: 20%; height: 30px; padding-top: 10px; font-size: 15px;">
  		<a href="<%=request.getContextPath() %>/">Quản lý danh mục</a> &raquo;
  		Danh sách danh mục
  	</div>
    <center>
  	<p align="center">
		<b><font color="#000080" face="Tahoma" size="4">
			DANH SÁCH ALBUM
		</font></b><br/><br/>
  	</p>
	<div align="center"><%=msg %></div>
	<form name="frm_show" method="post">
		<input type="hidden" name="action" value="add_category"/>
		<input type="hidden" name="id" />
		
		<table width="60%" cellpadding="0" cellspacing="0" border="0">
  			<tr>
  				<td colspan="9" align="right" class="header">
  					<span class="GEE3RVNDMU">
  						Tên danh mục: 
  						<input type="text" name="txtName" size="30"/>
  						Vị trí: 
  						<input type="text" name="txtPosition" size="3"/>
						<input type="submit" value="Thêm mới" class="blogg-button GEE3RVNDNU"/>
					</span>
  				</td>
  			</tr>
  		</table>
  		<br/>
  		<table width="60%" cellpadding="0" cellspacing="0" border="0" height="30px">
	  		<tr bgcolor="#EEEEEE" height="30px">
		  		<td width="1px" bgcolor="#fff"></td>
		  		<td align="center" class="header">
		  			STT
		  		</td>
		  		<td width="1px" bgcolor="#fff"></td>
		  		<td align="center" class="header">
		  			Tên danh mục
		  		</td>
		  		<td width="1px" bgcolor="#fff"></td>
		  		<td align="center" class="header">
		  			Vị trí
		  		</td>
		  		<td width="1px" bgcolor="#fff"></td>
		  		<td class="header" align="center">
		  			Ngày tạo
		  		</td>
		  		<td width="1px" bgcolor="#fff"></td>
		  		<td class="header" align="center">
		  			Sửa
		  		</td>
		  		<td width="1px" bgcolor="#fff"></td>
		  		<td class="header" align="center">
		  			Xóa
		  		</td>
	  		</tr>
   <%
		if(vCate != null && vCate.size() > 0){
			ClipCategory cate = null;
			int size = vCate.size();
			for(int i = 0; i < size; i++){
				cate = (ClipCategory)vCate.elementAt(i);
				if(cate != null){
   %>
	   				<tr height="30px">
				  		<td bgcolor="#fff"></td>
				  		<td class="list" align="center">
				  			<%=i + 1 %>
				  		</td>
				  		<td bgcolor="#fff"></td>
				  		<td class="list">
				  			<a href="index.jsp?cateId=<%= cate.getId() %>">
				  				<%= cate.getName() %>
				  			</a>
				  		</td>
				  		<td bgcolor="#fff"></td>	
				  		<td align="center" class="list">
				  			<%= cate.getPosition() %>
				  		</td>
				  		<td bgcolor="#fff"></td>
				  		<td class="list" align="center" >
				  			<%=DateProc.Timestamp2DDMMYYYY(cate.getGenDate()) %>
				  		</td>
				  		<td bgcolor="#fff"></td>
				  		<td class="list" align="center">
			  				<a href="javascript:void(0);" onclick="popupwindow('cate_edit.jsp?id=<%= cate.getId() %>','Sửa thông tin danh mục', 450, 350);">Sửa</a>
				  		</td>
				  		<td bgcolor="#fff"></td>
				  		<td class="list" align="center">
			  				<a href="javascript:void(0);" onclick="if(confirm('Bạn có chắc chắn muốn xóa album này ??? ')){frm_show.id.value='<%= cate.getId() %>';frm_show.action.value='delete';frm_show.submit(); }">Xóa</a>
				  		</td>
			  		</tr>
   <%
   				}
   			}
   		}
   %>
   		</table>
		<div id="dialog-modal" title="Mã nhúng" style="display: none;"></div>
   	</form>
   </center>
  </body>
</html>
